<template>
  <div style="background-color: #FFFFFF; padding: 20px;">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="标题" prop="db_title">
        <el-input v-model="ruleForm.db_title"></el-input>
      </el-form-item>

      <el-form-item label="库类型" prop="db_type">
        <el-select v-model="ruleForm.db_type" placeholder="请选择库类型" style="width: 100%;">
          <el-option
            v-for="(item,index) in optionsDbType"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="库名" prop="db_name">
        <el-input v-model="ruleForm.db_name"></el-input>
      </el-form-item>

      <el-form-item label="IP地址" prop="db_ip">
        <el-input v-model="ruleForm.db_ip"></el-input>
      </el-form-item>

      <el-form-item label="端口号" prop="db_port">
        <el-input v-model="ruleForm.db_port"></el-input>
      </el-form-item>

      <el-form-item label="用户名" prop="db_user">
        <el-input v-model="ruleForm.db_user"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="db_password">
        <el-input v-model="ruleForm.db_password"></el-input>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="ruleForm.status" placeholder="请选择状态" style="width: 100%;">
          <el-option
            v-for="(item,index) in optionsCommon"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm()">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>
<script>
  export default {
    name: 'Adddatabase',
    data() {
      return {
        optionsCommon: [],  // 存储CommonChoices实际数据的列表
        optionsDbType: [],  // 数据库类型

        EditUrl: this.$http.databaseURL,  // get编辑的URL
        EditIdStr: 'database_id',  // 模块id字符串
        pushObjName: 'database',  // 成功后跳转的路由

        ruleForm: {
          db_type: '',
          db_title: '',
          db_name: '',
          db_ip: '',
          db_port: '', //端口号
          db_user: '',
          db_password: '',
          status: '',
        },  //
        rules: {
          db_type: [
            {required: true, message: '请选择数据库类型', trigger: 'blur'}
          ],
          db_title: [
            {required: false, message: '请输入标题', trigger: 'blur'},
            {min: 0, max: 100, message: '长度在 0 到 100 个字符', trigger: 'blur'}
          ],
          db_name: [
            {required: true, message: '请输入库名', trigger: 'change'}
          ],
          db_ip: [
            {required: false, message: '请输入ip', trigger: 'blur'},
            {min: 0, max: 20, message: '长度在 0 到 20 个字符', trigger: 'blur'}
          ],
          db_port: [
            {required: false, message: '端口号', trigger: 'blur'},
            {min: 0, max: 6, message: '长度在 0 到 6 个字符', trigger: 'blur'}
          ],

          db_user: [
            {required: false, message: '请输入用户名', trigger: 'blur'},
            {min: 0, max: 20, message: '长度在 0 到 20 个字符', trigger: 'blur'}
          ],
          db_password: [
            {required: false, message: '请输入密码', trigger: 'blur'},
            {min: 0, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur'}
          ],

          status: [
            {required: true, message: '请选择状态', trigger: 'change'}
          ],
        }
      };
    },
    created() {
      this.getEditData();   // get回将要编辑的数据
      this.optionsCommon = this.getChoices.common;
      this.optionsDbType = this.getChoices.database.db_type;
    },
    methods: {
      submitForm() {
        this.$myTool.submitForm(this)
      },

      // get回将要编辑的数据
      getEditData() {
        this.$myTool.getEditData(this)
      },

      // get将要编辑的数据
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },

    computed: {
      getChoices() {
        return this.$store.state.choicesInfo
      }
    },
  }
</script>
